var typeOf = require('typeof');
var clone = require('clone');
var xss = require('xss');
var indexAvatar = '';
var pichost = 'http://' + config.imagehost + '/images/';

function getimghost() {
  if (indexAvatar == '') return (indexAvatar = 1); 
  if (indexAvatar >= 1 && indexAvatar <= 8) return indexAvatar++; 
  return (indexAvatar = "");
}
var utils = module.exports = {
  getPaging: function(query) {
    var page = parseInt(query.page) || 1,
    count = parseInt(query.count) || 20;
    return {
      page: page <= 0 ? 1: page,
      count: count > 20 ? 20: count,
      skip: (page - 1) * count
    };
  },
  formatResult: function(code, data) {
    if (!data) {
      data = code;
      code = 200;
    }
    var rets = {};
    if (typeOf(data) === 'array') {
      rets.count = data.length;
      rets.data = data;
    } else {
      rets.data = data;
    }
    return {
      status: code,
      body: rets
    };
  },
  fixUser: function(ret) {
    ret.forEach((item) => {
      if (item.userid && typeOf(item.userid) === 'object') {
        item.author = clone(item.userid);
        item.author.avatar = utils.getUrl(item.author.avatar);
        delete item.userid;
      }
      if(item.avatar){
        item.avatar = utils.getUrl(item.avatar);
      }
    })
  },
  getUrl: function(pid) {
    var index = getimghost();
    var path = config.imagehost.replace(/img(.*?)\./, 'img' + index + '.');
    return 'http://' + path + '/avatar/small/' + pid + '.png';
  },
  getArtUrl: function(pid) {
    var index = getimghost();
    var path = config.imagehost.replace(/img(.*?)\./, 'img' + index + '.');
    return 'http://' + path + '/avatar/big/' + pid + '.png';
  },
  setTime:function(data) {
    var D = new Date(data['created_at']);
    var hour = D.getHours(),
      Minutes = D.getMinutes();
    var since;
    if (hour >= 12 && hour < 18) since = '下午';
    if (hour >= 18 && hour < 20) since = '傍晚';
    if (hour >= 20 && hour < 24) since = '夜晚';
    if (hour >= 0 && hour < 6) since = '凌晨';
    if (hour >= 6 && hour < 9) since = '早晨';
    if (hour >= 9 && hour < 12) since = '上午';
    since = since + ' ' + hour + '时' + Minutes + '分';
    data['created_at'] = D.getFullYear() + '-' + (D.getMonth() + 1) + '-' + D.getDate() + ' ' + since;
  },
  setDay:function(data) {
    var D = new Date(data['created_at']);
    data['created_at'] = D.getFullYear() + '-' + (D.getMonth() + 1) + '-' + D.getDate();
  },
  isEmptyObject : function(obj) {
    for (var i in obj) {
      return false;
    }
    return true;
  },
  getpics : function(size, counts, data) {
    if (utils.isEmptyObject(data) || !data) return false;
    var n = 0,
      ret = [];
    for (var i in data) {
      ret.push(pichost + size + '/' + data[i]);
      n++;
      if (n === counts) break;
    }
    if (ret.length === 1) return ret[0];
    return ret;
  },
  getImgs : function(html) {
    var imgReg = /<img.*?(?:>|\/>)/gi;
    var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
    var imgs = [];
    var arr = html.match(imgReg);
    if (!arr) {
      return imgs;
    }
    for (var i = 0; i < arr.length; i++) {
      var src = arr[i].match(srcReg);
      if (src && src[1] && !(/sound\.png/).test(src[1])) {
        imgs.push(src[1].replace(/\?.*$/g, ''));
      }
    }
    return imgs;
  },
  getWavs : function(html) {
    var imgReg = /<img.*?(?:>|\/>)/gi;
    var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
    var imgs = [];
    var arr = html.match(imgReg);
    if (!arr) {
      return imgs;
    }
    for (var i = 0; i < arr.length; i++) {
      var src = arr[i].match(srcReg);
      if (src && src[1] && (/sound\.png/).test(src[1])) {
        imgs.push(src[1].replace(/\?.*$/g, ''));
      }
    }
    return imgs;
  },
  feedItemFix: function(item, notSetTime, notDiary) {
    if (!notSetTime) {
      utils.setTime(item);
    }
    item.img = utils.getpics(800, 1, item.filelist);
    item.avatarUrl = utils.getUrl(item.avatar);
    //写一个提取html富文本中第一张图片的函数，然后赋值给item.img
    var img = utils.getImgs(item.content)[0];
    item.img = img ? img + '?w=800&h=800' : item.img;
    item.wav = utils.getWavs(item.content)[0];
    if (!notDiary) {
      item.content = xss(item.content, {
        whiteList: {
          img:['src']
        },
        stripIgnoreTag: true
      });
    }
    item.content = item.content.replace(/(&nbsp;|\s\s+|\s$|^\s)/g, '');
    //item.content = item.content.length > 150 ? item.content.slice(0, 150) + '...' : item.content;
  }
};

